package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DeptTree;
import com.ruoyi.system.tools.BooleanMessage;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeptMapper;
import com.ruoyi.system.domain.Dept;
import com.ruoyi.system.service.IDeptService;
import com.ruoyi.common.core.text.Convert;
import java.util.Map;

/**
 * 单位信息Service业务层处理
 * 
 * @author dy
 * @date 2024-05-27
 */
@Service
public class DeptServiceImpl implements IDeptService 
{
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询单位信息
     * 
     * @param deptid 单位信息主键
     * @return 单位信息
     */
    @Override
    public Dept selectDeptByDeptid(String deptid)
    {
        return deptMapper.selectDeptByDeptid(deptid);
    }

    /**
     * 查询单位信息列表
     * 
     * @param dept 单位信息
     * @return 单位信息
     */
    @Override
    public List<Dept> selectDeptList(Dept dept)
    {
        return deptMapper.selectDeptList(dept);
    }

    /**
     * 新增单位信息
     * 
     * @param dept 单位信息
     * @return 结果
     */
    @Override
    public int insertDept(Dept dept)
    {
        return deptMapper.insertDept(dept);
    }

    /**
     * 修改单位信息
     * 
     * @param dept 单位信息
     * @return 结果
     */
    @Override
    public int updateDept(Dept dept)
    {
        return deptMapper.updateDept(dept);
    }

    /**
     * 批量删除单位信息
     * 
     * @param deptids 需要删除的单位信息主键
     * @return 结果
     */
    @Override
    public int deleteDeptByDeptids(String deptids)
    {
        return deptMapper.deleteDeptByDeptids(Convert.toStrArray(deptids));
    }

    /**
     * 删除单位信息信息
     * 
     * @param deptid 单位信息主键
     * @return 结果
     */
    @Override
    public int deleteDeptByDeptid(String deptid)
    {
        return deptMapper.deleteDeptByDeptid(deptid);
    }

    @Override
    public List<Map<String,Object>> CustomList(Dept dept){
        return deptMapper.CustomList(dept);
    }

    @Override
    public Map<String,Object>CustomOne(String deptid){
        return deptMapper.CustomOne(deptid);
    }

    // 查询到当前审核单位的下级 和本身的上级
    @Override
    public String  returnxiaji(String 审核单位,String 底层单位){  // 第一次底层进站
        //
        Dept mmone  = deptMapper.selectDeptByDeptid(底层单位);
        if(mmone.getParentid().equals(审核单位)){// 提交单位就是当前单位的下级
            return mmone.getDeptid();
        }else{
           return  returnxiaji(审核单位,mmone.getParentid());
        }
    }

    // 查询到当前审核单位是否是提交单位的上级或者提交单位
    @Override
    public boolean returnshangji(String 提交单位,String 审核单位){  // 第一次底层进站
        //
        if(提交单位.equals(审核单位)){
            return true;
        }
        // 提交单位是专家中心 证明已经找到最上级别了 所以 审核单位不是这个单位的上级
        if(提交单位.equals(RuoYiConfig.getZjzxdeptid())){
            return false;
        }
        Dept mmone  = deptMapper.selectDeptByDeptid(提交单位);
        return returnshangji(mmone.getParentid(),审核单位);
    }

    @Override
    public List<Map<String, Object>> selectLianxifangshi(String deptid) {
        return deptMapper.selectLianxifangshi(deptid,"000");
    }

    /**
     * @return List<DeptTree>
     */
    @Override
    public List<DeptTree> selectDeptTreeList() {
        List<Dept> deptList = deptMapper.selectDeptTreeList();
        List<DeptTree> deptTrees = initDeptTree(deptList);
        return deptTrees;
    }
    /**
     * @return List<DeptTree>
     */
    @Override
    public List<DeptTree> XiaoxideptTreeList(String deptid,String parentid) {
        List<Dept> deptList = deptMapper.XiaoxideptTreeList(deptid,parentid);
        List<DeptTree> deptTrees = initDeptTree(deptList);
        return deptTrees;
    }

    /**
     * 对象转单位树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<DeptTree> initDeptTree(List<Dept> deptList)
    {
        List<DeptTree> deptTrees = new ArrayList<DeptTree>();
        for (Dept dept : deptList)
        {
            DeptTree deptTree = new DeptTree();
            deptTree.setId(dept.getDeptid());
            deptTree.setpId(dept.getParentid());
            deptTree.setMmpid(dept.getParentid());
            deptTree.setName(dept.getDeptname());
            deptTree.setTitle(dept.getDeptname());
            deptTree.setJigoujibie(dept.getDeptlevel());
            deptTrees.add(deptTree);
        }
        return deptTrees;
    }

    @Override
    public List<DeptTree> TuikuandeptTreeList(String deptid) {
        List<Dept> deptList = deptMapper.TuikuandeptTreeList(deptid);
        List<DeptTree> deptTrees = initDeptTree(deptList);
        return deptTrees;
    }

    @Override
    public List<Dept> findYiquerenDeptByPiciAndParentId(String parentid,String piciid){
        return deptMapper.findYiquerenDeptByPiciAndParentId(parentid,piciid);
    }

    @Override
    public BooleanMessage checkDeptInfo(Long userId) {
        boolean ok = true;
        Dept d = deptMapper.checkDeptInfo(userId);

        //专家中心不判断
        if(d.getDeptid().equals("000"))return BooleanMessage.getSuccessMessage("");

        //经办人，经办人办公电话，经办人手机号码，开户行，开户名称，银行账号，财务联系人，财务联系人办公电话，财务联系人手机号码
        if(StringUtils.isEmpty(d.getKaihuhang())||StringUtils.isEmpty(d.getKaihuming()) || StringUtils.isEmpty(d.getYinhangzhanghao()) ||
                StringUtils.isEmpty(d.getJingbanren()) || StringUtils.isEmpty(d.getJingbanrenbangongdianhua()) || StringUtils.isEmpty(d.getJingbanrenshouji()) ||
                StringUtils.isEmpty(d.getCaiwurenyuan()) || StringUtils.isEmpty(d.getCaiwurenyuanbangongdianhua()) || StringUtils.isEmpty(d.getCaiwurenyuanshouji())){
            return BooleanMessage.getErrorMessage("请完善单位相关信息");
        }
        return BooleanMessage.getSuccessMessage("");
    }

    @Override
    public List<Dept> findSubDept(String DeptId){

        Dept mmDept = new Dept();
        mmDept.setParentid(DeptId);
        mmDept.setStatus(new Long(1));

        List<Dept> mmDeptList = selectDeptList(mmDept);
        List<Dept> list = new ArrayList<>();
        list.add(selectDeptByDeptid(DeptId));
        List<Dept> mmXiajiDeptList = findKonggeCengJi(mmDeptList);
        list.addAll(mmXiajiDeptList);
        return list;
    }

    public List<Dept> findKonggeCengJi (List<Dept> ppDeptList){
        List<Dept> mmDeptList = new ArrayList<>();
        for(int i=0;i<ppDeptList.size();i++){
            Dept mmDept = new Dept();
            mmDept.setParentid(ppDeptList.get(i).getDeptid());
            mmDept.setStatus(new Long(1));
            List<Dept> mmXiajiDeptList = selectDeptList(mmDept);

            List<Dept> mmXiashuList = new ArrayList<>();

            if(mmXiajiDeptList.size()>0){
                mmXiashuList = findKonggeCengJi(mmXiajiDeptList);
                ppDeptList.addAll(mmXiashuList);
            }
        }
        return ppDeptList;
    }
}
